import { getUserInfo, setUserInfo, getToken, removeToken } from "../../utils/auth.js"
import { Jump } from '@/libs/fn/jump.js'
import cookie from '@/utils/auth.js'
import store from '@/store/index.js'

export default {
	namespaced: true,
	state: {
		isLogin: false,
		token: cookie.get('Token') || '',
		DeciceToken: cookie.get('DeciceToken') || '',
		userinfo: cookie.get('UserInfo'),
		userbalance: {},
		unreadcount: 0, // 未读
		userBackList: [], //   用户收款账户列表
		backSelf: {}, // 个人账户
		backPublic: {}, // 对公账户
		companyInfo: {}, // 个人公司信息
		selectedCardId:0, //  当前选中银行卡ID
		farmActivationCode:cookie.get('FarmActivationCode') ||'',// 农场激活卡
	},
	mutations: {
		set_farmActivationCode(state,val){
			state.farmActivationCode = val;
			cookie.set('FarmActivationCode', val)
		},
		//登陆TOKEN
		SET_TOKEN(state, token) {
			state.token = token;
			cookie.set('Token', token)
		},
		set_deciceToken(state, token) {
			state.DeciceToken = token;
			cookie.set('DeciceToken', token)
		},
		SET_SELECTEDCARDID(state,id){
			state.selectedCardId = id;
		},
		SET_COMPANYINFO(state, data) {
			state.companyInfo = data;
		},
		SET_BACKPUBLIC(state, data) {
			state.backPublic = data;
		},
		SET_BACKSELF(state, data) {
			state.backSelf = data;
		},
		SET_USERBACKLIST(state, list) {
			state.userBackList = list;
		},
		SET_UNREAD(state, acount) {
			state.unreadcount = acount;
		},
		SET_LOGIN(state, status) {
			state.isLogin = status;
		},
		SET_CARD(state, list) {
			state.cardList = list
		},
		
		//用户信息
		SET_USERINFO(state, info) {
			state.userinfo = info;
			cookie.set('UserInfo', info)
		},
		//用户余额
		SET_USERBALANCE(state, balance) {
			console.log('balance===>', balance)
			state.userbalance = balance;
		},
		//退出登陆
		REMOVE_TOKEN(state) {
			state.backSelf = {};
			state.backPublic = {};
			state.set_loginToken = '';
			cookie.remove('Token');
			Jump('/pages/login/login');
			state.userinfo = {};
			// state.userbalance = {};
		},
	},
	getters: {

	},
	actions: {
		exit({ state, commit }) {
			return new Promise((resolve, reject) => {
				commit('REMOVE_TOKEN')
				store.commit('address/CLEAR')
				store.commit('goods/CLEAR')
				setTimeout(() => {
					resolve(true)
				}, 1000)
			})
		},
	}
}